package com.huawei.cloud.user.service;

import com.huawei.cloud.user.Dao.UserDao;
import com.huawei.cloud.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.MD5Util;

import java.io.Serializable;
import java.util.List;

/**
 * Created by cWX183898 on 2017/3/3.
 */
@Service
@Transactional()
public class UserService {

    @Autowired
    private UserDao userDao;


    public User save(User user) {
        user.setPassword(MD5Util.encode(user.getPassword(), "UTF-8"));
        return userDao.save(user);
    }

    public User readByUsername(String username) {
        return userDao.readByUsername(username);
    }

    public boolean validate(String username, String pasword, StringBuilder error) {
        User user = readByUsername(username);
        if (user == null) {
            error.append("用户名或密码错误！");
            return false;
        }
        String encodePassword = MD5Util.encode(pasword, "UTF-8");
        if (encodePassword == null || !encodePassword.equals(user.getPassword())) {
            error.append("用户名或密码错误！");
            return false;
        }
        return true;
    }
}
